package com.dean.action;

import com.dean.entity.DataQueryEntity;
import com.dean.entity.DeanDbConnInfo;
import com.dean.entity.DeanResponse;
import com.dean.entity.SysDbConnInfo;
import com.dean.exception.DeanRuntimeException;
import com.dean.service.CodeGenerateService;
import com.dean.service.DbQueryService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

/**
 * @ClassName DbQueryAction
 * @Description entity生成控制
 * @Author Administrator
 * @Date 2021/10/2315:31
 */
public class DbQueryAction {
    private final Logger logger = LoggerFactory.getLogger(DbQueryAction.class);

    private static class DbQueryActionHolder{
        private final static DbQueryAction INSTANCE = new DbQueryAction();
    }

    public static DbQueryAction getInstance(){
        return DbQueryAction.DbQueryActionHolder.INSTANCE;
    }

    public DataQueryEntity query(SysDbConnInfo connInfo, String sql,Integer pageSize){
        DataQueryEntity queryEntity = new DataQueryEntity();
        try {
            DbQueryService queryService = DbQueryService.getInstance();
            queryEntity = queryService.query(connInfo,sql,pageSize);
        }catch (Exception e){
            if(e instanceof DeanRuntimeException){
                DeanRuntimeException exception = (DeanRuntimeException) e;
                queryEntity.setCode(exception.getCode());
                queryEntity.setMsg(exception.getMsg());
            }else{
                logger.error(e.getMessage(),e);
                queryEntity.setCode(999);
                queryEntity.setMsg("未知错误类型,请联系管理员");
            }
        }
        return queryEntity;
    }


    public DataQueryEntity query(SysDbConnInfo connInfo, String sql){
        DataQueryEntity queryEntity = new DataQueryEntity();
        try {
            DbQueryService queryService = DbQueryService.getInstance();
            queryEntity = queryService.query(connInfo,sql);
        }catch (Exception e){
            if(e instanceof DeanRuntimeException){
                DeanRuntimeException exception = (DeanRuntimeException) e;
                queryEntity.setCode(exception.getCode());
                queryEntity.setMsg(exception.getMsg());
            }else{
                logger.error(e.getMessage(),e);
                queryEntity.setCode(999);
                queryEntity.setMsg("未知错误类型,请联系管理员");
            }
        }
        return queryEntity;
    }

    public DeanResponse<String> exportFile(DataQueryEntity queryEntity ,String filePath,String fileType){
        try {
            DbQueryService queryService = DbQueryService.getInstance();
            String fileName = queryService.exportFile(queryEntity,filePath,fileType);
            return DeanResponse.ok(fileName);
        }catch (Exception e){
            if(e instanceof DeanRuntimeException){
                DeanRuntimeException exception = (DeanRuntimeException) e;
                return DeanResponse.error(exception.getCode(),exception.getMsg());
            }else{
                logger.error(e.getMessage(),e);
                return DeanResponse.error(999,e.getMessage());
            }
        }
    }






}
